"""
基础Schema
"""
from datetime import datetime
from typing import Optional
from pydantic import BaseModel as PydanticBaseModel, ConfigDict


class BaseSchema(PydanticBaseModel):
    """基础Schema配置"""
    
    model_config = ConfigDict(
        from_attributes=True,  # 允许从ORM模型创建
        populate_by_name=True,
        arbitrary_types_allowed=True,
    )


class TimeStampSchema(BaseSchema):
    """时间戳Schema"""
    created_at: datetime
    updated_at: datetime


class ResponseBase(BaseSchema):
    """基础响应模型"""
    success: bool = True
    message: str = "操作成功"
    data: Optional[dict] = None


class PaginationParams(BaseSchema):
    """分页参数"""
    page: int = 1
    page_size: int = 20
    
    @property
    def offset(self) -> int:
        """计算偏移量"""
        return (self.page - 1) * self.page_size


class PaginationResponse(BaseSchema):
    """分页响应"""
    total: int
    page: int
    page_size: int
    pages: int
    items: list
    
    @classmethod
    def create(cls, items: list, total: int, page: int, page_size: int):
        """创建分页响应"""
        pages = (total + page_size - 1) // page_size
        return cls(
            total=total,
            page=page,
            page_size=page_size,
            pages=pages,
            items=items
        )







